﻿/*
package {
	import flash.display.Sprite;
	import flash.display.MovieClip;
	import flash.display.Loader;
	import flash.net.URLRequest;
	import flash.events.*;

	public class Main extends Sprite {
		private var mainImgMC:MovieClip;
		public var picBgMC:MovieClip;
		
		public function Main() {
			var mainImgMC:MovieClip = new MovieClip();
			mainImgMC.x = 5;
			mainImgMC.y = 5;
			mainImgMC.width = 550;
			mainImgMC.height = 500;
			this.addChild(mainImgMC);			
			
			var loader:Loader = new Loader();
			var urlReq:URLRequest = new URLRequest();
			//loader.addEventListener(Event.COMPLETE, completeHandler);
			urlReq.url = "Img1405296_x.jpg";
			loader.load(urlReq);
			
			mainImgMC.addChild(loader);
			trace(mainImgMC);
			//PicLoader.loadPic(mainImgMC,  "http://m1.biz.itc.cn/pic/x/96/52/Img1405296_x.jpg");
			
		}
		public function loadMainImg() {

		}
		
		public static function loadPic(mainImgMC:MovieClip, url:String){
			if(mainImgMC.picBgMC){
				mainImgMC.removeChild(mainImgMC.picBgMC);
			}
			//mainImgMC.picBgMC = new MovieClip();
			var loader:Loader = new Loader();
			var urlReq:URLRequest = new URLRequest();
			//loader.addEventListener(Event.COMPLETE, completeHandler);
			urlReq.url = url;
			loader.load(urlReq);
			
			mainImgMC.addChild(loader);
			trace(mainImgMC);
		}
		
		
	}
}
*/

package 
{
        import fl.transitions.*;
        import fl.transitions.easing.*;
        import flash.display.DisplayObject;
        import flash.display.Sprite;
        import flash.display.MovieClip;
        import flash.display.Loader;
		
        import flash.display.LoaderInfo;
        import flash.text.*;
        import flash.system.System;
        import flash.events.MouseEvent;
        import flash.display.Stage;
        import flash.events.Event;
        import flash.xml.XMLDocument;
        import flash.net.URLLoader;
        import flash.net.URLRequest;
        import flash.filters.GlowFilter;
        import flash.utils.Timer;
        import flash.events.TimerEvent;
        import fl.controls.Button;
        import flash.geom.*;

        public class Main extends Sprite 
        {
                //定义属性
                private var firstLoad:Boolean =true;
                private var rect:Sprite;
                private var rectMask:Sprite =new Sprite;
                private var ThumbContainer:Sprite= new Sprite;
                private var mc:MovieClip = new MovieClip;
                private var num:uint = 0;
                private var len:uint;
                private var XmlUrl:URLRequest;
                private var XmlLoader:URLLoader;
                private var PicUrl:URLRequest;
                private var ThumbUrl:URLRequest;
                private var loader:Loader;
                private var Text:TextField;
                private var BtnPrev:Button;
                private var BtnNext:Button;
                private var _picGlowFilter:GlowFilter = new GlowFilter(0x999999,1,3,3,3,2,false,false);
                private var _glowFilter:GlowFilter = new GlowFilter(0x000000,.8,2,2,4,1,true,false);
                var PicUrlAry:Array = new Array();
				var TPicUrlAry:Array = new Array();
                var PicTextAry:Array = new Array();

                public function Main() 
                { 		
                        init();//初始化
                }
                private function init():void 
                {
                        //初始化组件
                        BtnPrev = new Button();
                        BtnPrev.label="向上滚动";
                        BtnPrev.move(550,100);
                        BtnPrev.addEventListener(MouseEvent.MOUSE_DOWN,preHandler);
                        BtnPrev.addEventListener(MouseEvent.MOUSE_OVER,onpreHandler);
                        BtnPrev.addEventListener(MouseEvent.MOUSE_OUT,outpreHandler);
                        addChild(BtnPrev);

                        BtnNext = new Button();
                        BtnNext.label="向下滚动";
                        BtnNext.move(550,220);
                        BtnNext.addEventListener(MouseEvent.MOUSE_DOWN,nextHandler);
                        BtnNext.addEventListener(MouseEvent.MOUSE_OVER,onnextHandler);
                        BtnNext.addEventListener(MouseEvent.MOUSE_OUT,outnextHandler);
                        addChild(BtnNext);        
                        
                        //处理XML文件，处理完成后加载第一张大图和缩略图
                        XmlUrl = new URLRequest("info.xml");
                        XmlLoader = new URLLoader();
                        XmlLoader.load(XmlUrl);
                        XmlLoader.addEventListener(Event.COMPLETE,completeHandler);
                        
                        //缩略图的遮罩
						/*
                        rectMask.graphics.beginFill(0x000000,1);
                        rectMask.graphics.drawRect(50,550,500, 385);
                        rectMask.graphics.endFill();
                        */
                        
                }
                
                //xml载入完成
                function completeHandler(event:Event)
                {
                        var Xml:XML = new XML(event.target.data);
						var picNodes :XMLList  = Xml.elements("item");
						len = picNodes.length();
						
                        for(var i:uint=0;i<len;i++){															
                                PicUrlAry.push((picNodes[i].@url8).toString());
								TPicUrlAry.push((picNodes[i].@url).toString());
                                PicTextAry.push((picNodes[i].@t).toString());
                        }
                        //载入第一张图片和缩略图
                        loadPic(num);
                        loadThumb();
                }
                        
                //载入大图
                function loadPic(num:uint):void
                {
                        PicUrl = new URLRequest(PicUrlAry[num]);
                        loader = new Loader();
                        loader.contentLoaderInfo.addEventListener(Event.COMPLETE,PicLoadcomplete);
                        loader.load(PicUrl);
                        showText(num);
                } 
				
                //载入大图完成后，显示遮罩
                function PicLoadcomplete(evt:Event):void{
                        var picWidth = evt.target.content.width;
                        var picHeight = evt.target.content.height;
                        mc.addChildAt((evt.target.content), 0);//加载在最底层
            			addChildAt(mc,1);
                        //RandomInMask(mc);//随机遮罩显示
                        if(!firstLoad){
                                mc.removeChildAt(1);//移除上层的图片。
                        }
                        firstLoad = false;
                }
                //随机遮罩显示大图
                function RandomInMask(_mc:MovieClip){
                        var ran:Number;
                        ran =Math.round( Math.random() * 10 );
                        switch(ran){
                                case 1 :TransitionManager.start(_mc,{type:Blinds, direction:Transition.IN, duration:2, easing:None.easeNone, numStrips:10, dimension:0}); break;
                                case 2 :TransitionManager.start(_mc,{type:Blinds, direction:Transition.IN, duration:2, easing:None.easeNone, numStrips:10, dimension:1}); break;
                                case 3 :TransitionManager.start(_mc, {type:Iris, direction:Transition.IN, duration:2, easing:Strong.easeOut, startPoint:1, shape:Iris.CIRCLE}); break;
                                case 4 :TransitionManager.start(_mc, {type:Iris, direction:Transition.IN, duration:2, easing:Strong.easeOut, startPoint:2, shape:Iris.SQUARE}); break;
                                case 5 :TransitionManager.start(_mc, {type:Iris, direction:Transition.IN, duration:2, easing:Strong.easeOut, startPoint:5, shape:Iris.CIRCLE}); break;
                                case 6 :TransitionManager.start(_mc, {type:Iris, direction:Transition.IN, duration:2, easing:Strong.easeOut, startPoint:8, shape:Iris.SQUARE}); break;
                                case 7 :TransitionManager.start(_mc, {type:Fade, direction:Transition.IN, duration:9, easing:Strong.easeOut});
                                case 8 :TransitionManager.start(_mc, {type:PixelDissolve, direction:Transition.IN, duration:2, easing:Regular.easeIn, xSections:10, ySections:10}); break;
                                case 9 :TransitionManager.start(_mc, {type:Zoom, direction:Transition.IN, duration:2, easing:Elastic.easeOut});
                                default :TransitionManager.start(_mc, {type:Wipe, direction:Transition.IN, duration:2, easing:None.easeNone, startPoint:1}); break;
                        }
                }                
                
                //载入小图
                function loadThumb():void
                {
                        for(var i:uint=0;i<len;i++){
                                ThumbUrl = new URLRequest(TPicUrlAry[i]);
                                loader = new Loader();
                                loader.y= 550;
                                loader.x= i * 96; 
                                loader.contentLoaderInfo.addEventListener(Event.COMPLETE, ThumbLoadcomplete);
                                loader.load(ThumbUrl);
                                loader.addEventListener(MouseEvent.MOUSE_DOWN,showPic);
                                ThumbContainer.addChild(loader);
                        }
                        //ThumbContainer.mask=rectMask;
						
                        addChild(ThumbContainer);                   
                }

                //缩略图载入完成后修改大小
                function ThumbLoadcomplete(evt:Event):void{
                        var loaderinfo:LoaderInfo = evt.target as LoaderInfo;
                        //loaderinfo.content.width = 103;
                        //loaderinfo.content.height = 84;
                        loaderinfo.content.filters = [_picGlowFilter];
                }
                //缩略图的侦听事件
                function showPic(event:MouseEvent):void{
                        var target:DisplayObject = event.target as DisplayObject;
                        num = ThumbContainer.getChildIndex(target);
                        loadPic(num);
                        //removeChildAt(1);
                }

                //显示文字
                function showText(num:uint){
                        rect = new Sprite();
                        rect.graphics.beginFill(0x000000,.5);
                        rect.graphics.drawRect(10,650,398,50);
                        rect.graphics.endFill();
                        Text = new TextField();
                        Text.text=PicTextAry[num];
                        Text.width=328;
                        Text.height=30;
                        Text.textColor=0xFFFFFF;
						
                        Text.x=32;
                        Text.y=300;
                        Text.autoSize="left";
                        rect.addChild(Text);
                        addChild(rect);
                }

                //点击上一个
                function preHandler(event:MouseEvent):void
                {
                        if(num>0){
                                num--;
                                loadPic(num);
                        }else{ num=len-1; loadPic(num); }
                        //removeChildAt(1);//卸载之前显示的图片                        
                }
                //鼠标放在“上一个”按钮上
                function onpreHandler(event:MouseEvent):void
                {
                        ThumbContainer.addEventListener(Event.ENTER_FRAME,upEnterFrame);
                }
                //鼠标离开“上一个”按钮
                function outpreHandler(event:MouseEvent):void
                {
                        ThumbContainer.removeEventListener(Event.ENTER_FRAME,upEnterFrame);
                }
                //缩略图下降
                function upEnterFrame(event:Event):void
                {
                        ThumbContainer.x +=5;
                }
                
                //下一个
                function nextHandler(event:MouseEvent):void 
                {        
                        if(num <len-1){                                
                                num++;
                                loadPic(num);
                        }else{ num=0 ; loadPic(num);}
                        //removeChildAt(1);
                }
                //鼠标放在“下一个”按钮上
                function onnextHandler(event:MouseEvent):void
                {
                        ThumbContainer.addEventListener(Event.ENTER_FRAME,downEnterFrame);
                }
                //鼠标离开“下一个”按钮
                function outnextHandler(event:MouseEvent):void
                {
                        ThumbContainer.removeEventListener(Event.ENTER_FRAME,downEnterFrame);
                }
                //缩略图上升
                function downEnterFrame(event:Event):void
                {
                        ThumbContainer.x -=5;
                }
        }
}